/**
 * Copyright (C) 2012 Qinmeiwei. All rights reserved.
 */
package com.Qinmeiwei.web.dao;

import java.io.InputStream;

import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.ProxoolFacade;
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.xml.sax.InputSource;


/**
 * The Class ProxoolConfigurator.
 * 数据库连接池初始化
 *
 * @author p.wang
 * @version 1.0, 2012-03-30
 */
public class ProxoolConfigurator implements InitializingBean, DisposableBean {
	
	/** The config location. */
	private Resource configLocation;
	
	/** The Constant log. */
	private static final Logger log = LoggerFactory.getLogger(ProxoolConfigurator.class);
	
	/**
	 * Sets the config location.
	 *
	 * @param configLocation the new config location
	 */
	public void setConfigLocation(Resource configLocation) {
		this.configLocation = configLocation;
	}

	/* (non-Javadoc)
	 * @see org.springframework.beans.factory.DisposableBean#destroy()
	 */
	@Override
	public void destroy() throws Exception {
		ProxoolFacade.shutdown(0);
		log.warn("Proxool is shutdown");
	}

	/* (non-Javadoc)
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		if (this.configLocation != null) {
			InputStream is = this.configLocation.getInputStream();
			try {
				InputSource source = new InputSource(is);
				// 读取 proxool.xml 文件
				JAXPConfigurator.configure(source, false);
				String[] aliases = ProxoolFacade.getAliases();
				for(String aliase : aliases){
					log.warn("Proxool is regist:{}", aliase);
				}
			} catch (ProxoolException e) {
				log.error("Problem configuring " + this.configLocation.getURL(), e);
			} finally {
				is.close();
			}
		}
	}

}
